// 成功响应
class SuccessModel {
  state: boolean;
  data: any;
  message: string;
  constructor(data: any = null, message: string = "success") {
    this.state = true;
    this.data = data;
    this.message = message;
  }
}

// 错误响应
class ErrorModel {
  state: boolean;
  error: API.ResponseError;
  message?: string;
  constructor(error: API.ResponseError = null, message?: string) {
    if (typeof error === "string") {
      message = error;
      error = null;
    }
    if (error instanceof Error && message === undefined) {
      message = error.message;
    }
    this.state = false;
    this.error = error;
    this.message = message || "error";
  }
}

export default {
  ok: (data?: any, message?: string) => new SuccessModel(data, message),
  fail: (error?: API.ResponseError, message?: string) =>
    new ErrorModel(error, message),
};
